package cn.innosmart.aq.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.Reminder;

/**
 * Created by David on 2016/1/12.
 */
public class ReminderListAdapter extends BaseAdapter {
    public boolean isModify;
    private ArrayList<Reminder> reminders;
    private Context context;
    private LayoutInflater mInflater;
    private View.OnClickListener onClickListener;
    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener;

    /**
     * 备忘提醒适配器构造函数
     *
     * @param context
     * @param reminders 备忘提醒列表
     */
    public ReminderListAdapter(Context context, ArrayList<Reminder> reminders) {
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        this.reminders = reminders;
    }

    @Override
    public int getCount() {
        return reminders.size();
    }

    @Override
    public Object getItem(int position) {
        return reminders.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.listview_reminder, null);
            holder.cbAssign = (CheckBox) convertView.findViewById(R.id.cb_assign);
            holder.tvBoxTitle = (TextView) convertView.findViewById(R.id.tv_box_title);
            holder.tvDelete = (TextView) convertView.findViewById(R.id.tv_delete);
            holder.rlBg = (RelativeLayout) convertView.findViewById(R.id.rl_bg);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Reminder reminder = reminders.get(position);
        holder.tvBoxTitle.setText(reminder.title);
        if (reminder.isEnable) {
            holder.cbAssign.setChecked(true);
        } else {
            holder.cbAssign.setChecked(false);
        }
        holder.tvDelete.setTag(reminder);
        holder.cbAssign.setTag(reminder);
        holder.rlBg.setTag(reminder);
        holder.rlBg.setOnClickListener(onClickListener);
        if (isModify) {
            if (onClickListener != null)
                holder.tvDelete.setOnClickListener(onClickListener);
            holder.tvDelete.setVisibility(View.VISIBLE);
            holder.cbAssign.setVisibility(View.GONE);
        } else {
            holder.tvDelete.setVisibility(View.GONE);
            holder.cbAssign.setVisibility(View.VISIBLE);
            if (onCheckedChangeListener != null)
                holder.cbAssign.setOnCheckedChangeListener(onCheckedChangeListener);
        }
        return convertView;
    }

    //设置勾选状态改变监听器
    public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }

    /**
     * 进入修改模式
     *
     * @param startModify
     */
    public void startModify(boolean startModify) {
        this.isModify = startModify;
        notifyDataSetChanged();
    }

    /**
     * 设置点击事件监听器
     *
     * @param onClickListener
     */
    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    /**
     * 刷新数据
     *
     * @param reminders
     */
    public void refresh(ArrayList<Reminder> reminders) {
        this.reminders = reminders;
        notifyDataSetChanged();
    }

    static class ViewHolder {
        TextView tvBoxTitle;
        CheckBox cbAssign;
        TextView tvDelete;
        RelativeLayout rlBg;
    }
}
